<?php
/**
 * Created by PhpStorm.
 * User: 图图
 * Date: 2021-5-11
 * Time: 9:11
 */

namespace app\manage\admin;
use app\admin\controller\Admin;
use app\common\builder\ZBuilder;
use app\common\model\ProjectCategory as Category_Model;
use think\Db;
use app\common\model\Project as Project_Model;

class ProjectCategory extends Admin
{

    //公告列表
    public function index(){
        // 读取数据
        $map = $this->getMap();
        // 排序
        $order = $this->getOrder();
        // 数据列表
        $data_list = Category_Model::alias('c')
            ->leftJoin('dp_qt_project_category p','c.pid=p.id')
            ->field('c.*,p.name parent_name')
        ->where($map)->order($order)->paginate();
        // 分页数据
        $page = $data_list->render();

        $btn_add = [
            'title' => '新增项目分类',
            'icon' => 'fa fa-fw fa-plus',
            'class' => 'btn btn-info',
            'href' => url('add'),
        ];

        $btn_edit = [
            'title' => '修改项目分类',
            'icon' => 'fa fa-fw fa-pencil',
            'class' => 'btn btn-info btn-sm',
            'href' => url('edit',['id'=>"__id__"]),
        ];


        //设置页面
        return ZBuilder::make('table')
            ->addColumn('id', 'ID', 'text')
            ->addColumn('sort', '排序', 'text')
            ->addColumn('name', '位置名称', 'text')
            ->addColumn('parent_name', '上级分类', 'text')
            ->addColumn('right_button', '操作', 'btn')
            ->setTableName('qt_project_category')// 指定数据表名
            ->addTopButton('add', $btn_add)// 添加顶部按钮
            ->addRightButton('edit', $btn_edit)// 添加顶部按钮
            ->addTopButton('delete')
            ->addTopButton('disable')
            ->addTopButton('enable')
            ->setPrimaryKey('id')// 设置主键名为username
            ->setPageTitle('项目列表')
            ->setRowList($data_list)// 设置表格数据
            ->setPages($page)// 设置分页数据
            ->fetch();
    }

    //添加广告位置
    public function add()
    {
        //判断是否提交信息
        if ($this->request->isPost()) {
            // 表单数据
            $data = $this->request->post();

            $result = $this->validate($data, 'Project');

            if ($page = Category_Model::create($data)) {

                $this->success('新增成功', 'index');
            } else {
                $this->error('新增失败');
            }
        } else {
            $list = Category_Model::column('id,name');
            return ZBuilder::make('form')
                ->addFormItems(
                    [
                        ['text', 'sort','','','',['排序号:']],
                        ['text', 'name','','','',['分类名称:']],
                        ['select','pid','请选择项目类型','',$list],
                    ]
                )
                ->setPageTitle('添加项目分类')
                ->fetch();
        }
    }


    //编辑公告
    public function edit($id = null)
    {
        //判断是否提交信息
        if ($this->request->isPost()) {
            // 表单数据
            $data = $this->request->post();
            // 验证
            $result = $this->validate($data, 'ProjectCategory');
            if (true !== $result) $this->error($result);

            if (Category_Model::where('id',$id)->update($data)) {
                $this->success('编辑成功', 'index');
            } else {
                $this->error('编辑失败');
            }
        } else {
            $info = Category_Model::get($id);
            $category_list = Category_Model::column('id,name');
            return ZBuilder::make('form')
                ->addFormItems(
                    [
                        ['text', 'sort','','','',['排序号:']],
                        ['text', 'name','','','',['分类名称:']],
                        ['select','pid','请选择上级','',$category_list],
                    ]
                )
                ->setFormData($info)
                ->setPageTitle('修改项目分类')
                ->fetch();
        }
    }

}